<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    //public $users;
    public function _initialize(){
        if(!session('admin')){
            $this->error('无权限',U('Login/login'));
            exit;
        }
    }


    function userlist(){
    	$usermodel=m('users');//生成模型对象
    	$local_users=$usermodel->order('id desc')->select();//select * from think_users order by desc 
        $this->users=$local_users;
        $this->display();//把当前对象所有全局变量传递到对应视图中，可以使用assign限制传递
        //$this->assign();
    }
   
   //分页
    function bsuserlist(){
    	$User = m('users'); // 实例化User对象
$count = $User->count();// 查询满足要求的总记录数
$Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)

$show = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
$list = $User->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('users',$list);// 赋值数据集
$this->assign('page',$show);// 赋值分页输出
$this->display(); // 输出模板
    }

    function users(){
    	$usermodel=m('users');//生成模型对象
    	$local_users=$usermodel->order('id desc')->select();//select * from think_users order by desc 
        $this->users=$local_users;
        $this->display();//把当前对象所有全局变量传递到对应视图中，可以使用assign限制传递
        //$this->assign();
    }
    function del($id){
        $usermodel=m('users');//生成一个users模型
        if($usermodel->delete($id)){
        	$this->success('delete success');
        }else{
        	$this->error('delete failed');
        }        
    }//id同名

    function setstar($id){
    	$usermodel=m('users');
        $data['isstar']=1;
        $data['isfirst']=1;
        if($usermodel->where("id=$id")->save($data)){
        	$this->success('success');
        }else{
        	$this->error('failed');
        }
    	    }

     function unstar($id){
	    $usermodel=m('users');
	    $usermodel->isstar=0;
	    $usermodel->isfirst=0;
	    $usermodel->id=$id;
	    if($usermodel->save()){
	    	$this->success('succcess');
	    }else{
	    	$this->error('failed');
	    }
}

    function setfirst($id){
    	$usermodel=m('users');
        $data['isfirst']=1;
        $data['isstar']=1;
        if($usermodel->where("id=$id")->save($data)){
        	$this->success('success');
        }else{
        	$this->error('failed');
        }
    	    }

function unfirst($id){
	    $usermodel=m('users');
	    $usermodel->isfirst=0;
	    $usermodel->isstar=0;
	    $usermodel->id=$id;
	    if($usermodel->save()){
	    	$this->success('succcess');
	    }else{
	    	$this->error('failed');
	    }
 }

   function updateuserinfo($id){
        $usermodel=m('users');
        $this->user=$usermodel->find($id);
        $this->display();
        //将查询到的用户信息user传递给模版updateuserinfo。html

   }
   

   function update(){
//获取updateuserinfo界面提交过来的数据，写入修改数据库   
//(1)$data=$_POST (2)$daat=I(post.) (3)create()利用$_POST数据创建一个对象
    $usermodel=D('users');
   	$usermodel->create();
   // 	if($_FILES['photo']['error']==0){
 		// $photoinfo=$this->upload();
  	// $usermodel->photo=$photoinfo['photo']['savename'];
   // 	}
   	   	if($usermodel->save()){
   		$this->success('success','users');
   	}else{
   		$this->error('failed');
   	}
   }

   public function upload(){
   $upload = new \Think\Upload();// 实例化上传类
   $upload->maxSize = 3145728 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
//$upload->savePath = ''; // 设置附件上传（子）目录
$upload->autoSub=false;
// 上传文件
$info = $upload->upload();
if(!$info) {// 上传错误提示错误信息
$this->error($upload->getError());
}else{// 上传成功
//$this->success('上传成功！');
	return $info;
}
}

 public function adduser(){
 	$usermodel=D('users');
   	if($usermodel->create()){
   		if($_FILES['photo']['error']==0){ //判断文件上传是否成功
   		
   		$fileName=$this->img();
   	    $usermodel->photo=$fileName;
   	}
   	   	if($usermodel->add()){
   		$this->success('success','users');
   	}else{
   		$this->error('failed');
   	}
   }else{
   	$this->error($usermodel->getError());
   }
    }

 function img(){
	$photoinfo = $this->upload();
	$filePath = './Public/Uploads/photo/';
	$oldFileName =$photoinfo['photo']['savename'];
	$oldFileFullName = $filePath."\\".$oldFileName;
	$newFileName ='thumb_'.$oldFileName;
	$newFileFullName = $filePath."\\".$newFileName;
	$image=new \Think\Image();
	$image->open($oldFileFullName);
	$image->thumb(100,100)->save($newFileFullName);
	return $newFileName;
 }  


}


